/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.sdju.chat;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.JOptionPane;


import java.awt.*;
import java.awt.event.*;
import javax.swing.* ;
/**
 *
 * @author 99132
 */
public class ChatLogin_UI extends javax.swing.JFrame {

    /**
     * Creates new form ChatClient_UI
     */
    public ChatLogin_UI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuBar1 = new java.awt.MenuBar();
        menu1 = new java.awt.Menu();
        menu2 = new java.awt.Menu();
        menuBar2 = new java.awt.MenuBar();
        menu3 = new java.awt.Menu();
        menu4 = new java.awt.Menu();
        jPanel1 = new javax.swing.JPanel();
        clientName = new javax.swing.JLabel();
        clientPasswords = new javax.swing.JLabel();
        SetIP = new javax.swing.JLabel();
        SetPort = new javax.swing.JLabel();
        loginJButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        clientNameTextField = new java.awt.TextField();
        clientPasswordsTextField = new java.awt.TextField();
        CurrIPTextField = new java.awt.TextField();
        CurrPortTextField = new java.awt.TextField();
        jLabel6 = new javax.swing.JLabel();
        RegistJButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        HelpJMenu = new javax.swing.JMenu();
        IPJMenuItem = new javax.swing.JMenu();
        PortJMenuItem = new javax.swing.JMenu();

        menu1.setLabel("File");
        menuBar1.add(menu1);

        menu2.setLabel("Edit");
        menuBar1.add(menu2);

        menu3.setLabel("File");
        menuBar2.add(menu3);

        menu4.setLabel("Edit");
        menuBar2.add(menu4);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        clientName.setText("用户名");

        clientPasswords.setText("密码");

        SetIP.setText("设置IP");

        SetPort.setText("设置端口");

        loginJButton.setText("登录");
        loginJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					loginJButtonActionPerformed(evt);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });

        jLabel5.setText("局域网聊天程序");

        clientNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clientNameTextFieldActionPerformed(evt);
            }
        });

        jLabel6.setText("1024-65535");

        RegistJButton.setText("注册");
        RegistJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegistJButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(clientPasswords, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(clientName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(SetIP)
                            .addComponent(SetPort))
                        .addGap(39, 39, 39)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                            .addComponent(clientNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(clientPasswordsTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(CurrIPTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(CurrPortTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 20, Short.MAX_VALUE)
                        .addComponent(jLabel6)
                        .addGap(25, 25, 25))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(RegistJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(loginJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(82, 82, 82))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(66, 66, 66)
                                .addComponent(clientName))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(clientNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(36, 36, 36)
                        .addComponent(clientPasswords))
                    .addComponent(clientPasswordsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SetIP)
                    .addComponent(CurrIPTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SetPort)
                    .addComponent(CurrPortTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RegistJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        HelpJMenu.setText("帮助");

        IPJMenuItem.setText("设置IP");
        IPJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IPJMenuItemActionPerformed(evt);
            }
        });
        HelpJMenu.add(IPJMenuItem);

        PortJMenuItem.setText("设置端口");
        HelpJMenu.add(PortJMenuItem);

        jMenuBar1.add(HelpJMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(118, 118, 118)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(162, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clientNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clientNameTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_clientNameTextFieldActionPerformed

    private void RegistJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RegistJButtonActionPerformed
        // TODO add your handling code here:
        new Regist_UI();
    }//GEN-LAST:event_RegistJButtonActionPerformed

    private void loginJButtonActionPerformed(java.awt.event.ActionEvent evt) throws IOException {//GEN-FIRST:event_loginJButtonActionPerformed
        // TODO add your handling code here:

        String name , Passwords  ;
        String ip ;
        String port ;
    //    ChatClient CC = new ChatClient() ;
        name = clientNameTextField.getText() ;
        Passwords = clientPasswordsTextField.getText() ;
        ip = CurrIPTextField.getText() ;
        port = CurrPortTextField.getText() ;
        
        boolean result ;
        result = Judgement(name ,  Passwords , ip ,  port) ; 
        
        if(result == true) {
        	JOptionPane.showMessageDialog(null,
					"登录成功", "提示",
					JOptionPane.INFORMATION_MESSAGE);
        	this.dispose();
        	ChatClient CC = new ChatClient();
        	CC.ip = ip ;
            CC.user = new User(name ,Passwords) ;
            CC.port = Integer.parseInt(port) ;
        	
        	
        }
        else {
        	JOptionPane.showMessageDialog(null,
					"请检查账号密码或者端口是否正确", "提示",
					JOptionPane.INFORMATION_MESSAGE);
        	
        	
        }
        
        
      /*  ChatClient CC = new ChatClient() ;
        CC.ip = ip ;
        CC.port = port ;
        CC.Username= name ;
        CC.Passwords = Passwords ;*/
        
        
        
        
    }//GEN-LAST:event_loginJButtonActionPerformed
    
    private Boolean Judgement(String name , String Passwords , String ip , String port) throws IOException {
    	boolean result = false;
    	
    	try {
    		int cport ;
    		cport = Integer.parseInt(port);
    		
    		String account = name;
    		String password = Passwords;
    		Map<String, String> map = new HashMap<String , String>();
    		String path = "D:\\test.txt";
    		File file = new File(path);
    		BufferedReader bReader = new BufferedReader(new FileReader(file));
    		String string =  "";
    		String pc [] = null;
    		String key = "";
    		String value = "";
    		while ((string = bReader.readLine()) != null) {
    		pc = string.split(",");
    		key = pc[0];
    		value = pc[1];
    		map.put(key, value);
    		}
    		
    		Set<String> keySet = map.keySet();
    		for (String string2 : keySet) {
    		System.out.println(string2);//账号
    		System.out.println(map.get(string2));//密码
    		if (account .equals(string2) && password.equals(map.get(string2))) {
    		result = true;
    		break;
    		}
    		}
    		/*if (result == false) {
    		System.out.println("密码错误");
    		}else {
    		System.out.println("账号密码正确");
    		}*/
    		bReader.close();

		} catch (NumberFormatException e) { //输入非数字
			/*JOptionPane.showMessageDialog(null, "端口设置不成功", "提示",
					JOptionPane.INFORMATION_MESSAGE);*/
		}
    	return result ;
    	
    	
    }

    private void IPJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IPJMenuItemActionPerformed
 // TODO add your handling code here:
    }//GEN-LAST:event_IPJMenuItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChatLogin_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChatLogin_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChatLogin_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChatLogin_UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChatLogin_UI().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.TextField CurrIPTextField;
    private java.awt.TextField CurrPortTextField;
    private javax.swing.JMenu HelpJMenu;
    private javax.swing.JMenu IPJMenuItem;
    private javax.swing.JMenu PortJMenuItem;
    private javax.swing.JButton RegistJButton;
    private javax.swing.JLabel SetIP;
    private javax.swing.JLabel SetPort;
    private javax.swing.JLabel clientName;
    private java.awt.TextField clientNameTextField;
    private javax.swing.JLabel clientPasswords;
    private java.awt.TextField clientPasswordsTextField;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton loginJButton;
    private java.awt.Menu menu1;
    private java.awt.Menu menu2;
    private java.awt.Menu menu3;
    private java.awt.Menu menu4;
    private java.awt.MenuBar menuBar1;
    private java.awt.MenuBar menuBar2;
    // End of variables declaration//GEN-END:variables

   /* private static class ActionListenerImpl implements ActionListener {

        public ActionListenerImpl() {
        }

        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null,
                    "输入你要连接的服务器IP\n"
                            + "(格式：XXX.XXX.XXX.XXX)"
                            +"输入你要连接服务器 的端口，端口应为（1024-65535）的整数", "如何设置",
                            JOptionPane.INFORMATION_MESSAGE);
        }
    }*/
}
